package com.umakr.ax.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 *
 * @author Mikey
 * @date 2017/7/1
 */
public class Mp3ConverterUtils {
    protected static final Log logger = LogFactory.getLog(Mp3ConverterUtils.class);
    public final static String COMMAND_AMR_TO_MP3 = "ffmpeg -i %s %s";
    public final static String COMMAND_AAC_TO_MP3 = "ffmpeg -i %s -acodec libmp3lame %s";
    public final static String COMMAND_AAC_TO_WAV = "ffmpeg -i %s -ar 16000 %s";
    /**public final static String COMMAND_AMR_TO_WAV = "ffmpeg -acodec libamr_nb -i %s %s";*/
    public final static String COMMAND_AMR_TO_WAV = "ffmpeg -i %s %s";
    public static void convertAmrToMp3(String srcPath,String toPath){
        try {
            Environment env =  WebUtil.getBean(Environment.class);
            String ffmpegPath = env.getProperty("ffmpeg.path");
            Runtime.getRuntime().exec(ffmpegPath+String.format(COMMAND_AMR_TO_MP3,srcPath,toPath));
        } catch (IOException e) {
            logger.error(e);
        }
    }
    public static void convertToMp3(String srcPath,String toPath){
        if(srcPath.endsWith(".amr")){
            convertAmrToMp3(srcPath,toPath);
        }else{
            convertAacToMp3(srcPath,toPath);
        }
    }
    public static void convertAacToMp3(String srcPath,String toPath){
        try {
            Environment env =  WebUtil.getBean(Environment.class);
            String ffmpegPath = env.getProperty("ffmpeg.path");
            Runtime.getRuntime().exec(ffmpegPath+String.format(COMMAND_AAC_TO_MP3,srcPath,toPath));
        } catch (IOException e) {
            logger.error(e);
        }
    }

    public static void convertAacToWav(String srcPath,String destPath){
        try {
            File file = new File(srcPath);
            if(!file.exists()) {
                logger.info("文件不存在："+srcPath);
            }
            File f = new File(destPath);
            if(f.exists()) {
                logger.info("音频文件已存在 删除");
                f.delete();
            }

            Environment env =  WebUtil.getBean(Environment.class);
            String ffmpegPath = env.getProperty("ffmpeg.path");
            //执行命令
            Process process = Runtime.getRuntime().exec(ffmpegPath + String.format(COMMAND_AAC_TO_WAV,srcPath,destPath));
            //
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            //输出结果，需要有这部分代码， 否则不能生产抽取的音频文件
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            process.destroy();
        } catch (IOException e) {
            logger.error(e);
        }
    }


    public static String convertToWav(String srcPath){
        try {
            Environment env =  WebUtil.getBean(Environment.class);
            File file = new File(srcPath);
            if(!file.exists()) {
                logger.info("文件不存在："+srcPath);
            }

            String destPath = srcPath.substring(0, srcPath.lastIndexOf(".")) + ".wav";
            File f = new File(destPath);
            if(f.exists()) {
                logger.info("音频文件已存在 删除");
                f.delete();
            }

            String ffmpegPath = env.getProperty("ffmpeg.path");
            Process process = null;
            if (srcPath.endsWith(".amr")){
                //执行命令
                process = Runtime.getRuntime().exec(ffmpegPath + String.format(COMMAND_AMR_TO_WAV,srcPath,destPath));
            }else{
                //执行命令
                process =  Runtime.getRuntime().exec(ffmpegPath + String.format(COMMAND_AAC_TO_WAV,srcPath,destPath));
            }

            process.waitFor();
            process.destroy();
            return destPath;
        } catch (Exception e) {
            logger.error(e);
        }
        return "";
    }

    public static void main(String[] args){
       // convertAacToMp3("D:\\work\\proj\\123.aac","D:\\work\\proj\\k.mp3");

        //convertAmrToMp3("D:\\work\\proj\\test.amr","D:\\work\\proj\\nn.mp3");
    }
}
